package com.zh.note.huawei.onehundred.文件目录大小;

import java.util.*;

/**
 * https://dream.blog.csdn.net/article/details/131579853
 * 递归
 */
public class Main {
    public static void main(String[] args) {
        // 数据输入
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt(); // 目录个数
        int n = sc.nextInt(); // 待查询目录id
        // 存储数据
        Map<Integer, List<Integer>> dataMap = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int id = sc.nextInt();
            int size = sc.nextInt();
            String subID = sc.next();
            dataMap.putIfAbsent(id, new ArrayList<>());
            List<Integer> data = dataMap.get(id);
            data.add(size);
            data.addAll(split(subID));
        }
        //计算值
        int res = dfs(n, dataMap);
        System.out.println(res);


    }

    private static int dfs(int targetId, Map<Integer, List<Integer>> dataMap) {

        int res = 0;
        if (dataMap.containsKey(targetId)) {
            List<Integer> data = dataMap.get(targetId);
            res = data.get(0);
            // 从第二个开始遍历
            for (int i = 1; i < data.size(); i++) {
                res += dfs(data.get(i), dataMap);
            }
        }
        return res;
    }

    /**
     * 对第三个输入的字符串进行拆分
     *
     * @param s
     * @return
     */
    private static List<Integer> split(String s) {
        List<Integer> res = new ArrayList<>();
        // 通过截取去掉左右括号
        String trimmedStr = s.substring(1, s.length() - 1);
        if (trimmedStr.isEmpty()) {
            return res;
        }
        String[] nums = trimmedStr.split(",");
        for (String num : nums) {
            res.add(Integer.parseInt(num));
        }
        return res;

    }
}
